<?php

/**
 * This is the model class for table "sd.property".
 *
 * The followings are the available columns in table 'sd.property':
 * @property string $id
 * @property string $title
 * @property integer $type
 *
 * The followings are the available model relations:
 * @property CategoryProperty[] $categoryProperties
 * @property GoodsProperty[] $goodsProperties
 * @property Property $parent
 * @property Property[] $properties
 * @property PropertyValue[] $propertyValues
 */
class Property extends CBaseActiveRecord
{
	private $_aTypes = array();

	/**
	 * @return array
	 */
	public function getTypes()
	{
		return array(
			1 => _('Фильтр'),
			2 => _('Стать'),
			3 => _('Размер'),
			4 => _('Цвет'),
			5 => _('Изготовитель'),
		);
	}

	public $values = array();

	/**
	 * @param array $values
	 */
	public function setValues($values)
	{
		$this->values = $values;
	}

	/**
	 * @return array
	 */
	public function getValues()
	{
		return $this->values;
	}



	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sd.property';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'required'),
			array('type', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>50),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, title, type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'categoryProperties' => array(self::HAS_MANY, 'CategoryProperty', 'property_id'),
			'goodsProperties' => array(self::HAS_MANY, 'GoodsProperty', 'property_id'),
			'properties' => array(self::HAS_MANY, 'Property', 'parent_id'),
			'propertyValues' => array(self::HAS_MANY, 'PropertyValue', 'property_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'type' => 'Type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('type',$this->type);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Property the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function saveRow($params)
	{
		if(empty($params['title']))
			return array();

		$oRow = null;

		if(!empty($params['property_id']) && $params['property_id'] > 0)
		{
			$oRow = $this->findByPk($params['property_id']);
		}

		if(!$oRow)
			$oRow = new self;

//		print_r($params); exit;

		$oRow->setAttributes(array(
		                          'title' => $params['title'],
		                          'type' => (!empty($params['type']) && $params['type'] > 0 ? $params['type'] : 1),
		                     ));

		$oRow->save();

		return $oRow;
	}

}
